Appendix B — Assignment 2 (Functions)

Instructions

  1. You may talk to a friend, discuss the questions and potential directions for solving them. However, you need to write your own solutions and code separately, and not as a group activity.

  2. Do not write your name on the assignment.

  3. Write your code in the Code cells and your answer in the Markdown cells of the Jupyter notebook. Ensure that the solution is written neatly enough to understand and grade.

  4. Use Quarto to print the .ipynb file as HTML. You will need to open the command prompt, navigate to the directory containing the file, and use the command: quarto render filename.ipynb --to html. Submit the HTML file.

  5. There are 5 points for clealiness and organization. The code should be commented and clearly written with intuitive variable names. For example, use variable names such as number_input, factor, hours, instead of a,b,xyz, etc.

  6. The assignment is worth 100 points, and is due on 14th October 2022 at 11:59 pm.

B.1 1(a)

Write a function that accepts a word, and a sentence as arguments, and returns the number of times the word occurs in the sentence.

Call the function, and print the returned value if the word is “sea”, and the sentence is “She sells sea shells on the sea shore when the sea is calm.” Note that this is just an example to check your function. Your function should work for any word and sentence.

(10 points)

B.2 1(b)

Ask the user to input a sentence. Use the function in 1(a) to find the word that occurs the maximum number of times in the sentence. Print the word and its number of occurences. If multiple words occur the maximum number of times, then you can print any one of them.

Check your program when the user inputs the sentence, “She sells sea shells on the sea shore when the sea is calm.”. Your program must print, “The word with the maximum number of occurences is ‘sea’ and it occurs 3 times.” Note that this is just an example to check your program. Your program must work for any sentence”

(20 points)

B.3 2(a)

Write a function that checks if an integer is prime. The function must accept the integer as an argument, and return True if the integer is prime, otherwise it must return False.

Call your function with the argument as 197.

(4 points)

B.4 2(b)

Write a function that checks if an integer is a factor of another integer. The function must accept both the integers as arguments, and return True if the integer is a factor, otherwise it must return False.

Call your function with the arguments as (19,85).

(3 points)

B.5 2(c)

Prompt the user to input a positive integer. Use the functions in 2(a) and 2(b) to print the prime factors of the integer. Your program should be no more than 4 lines (excluding the comments)

Check your program is the user inputs 190

(8 points)

B.6 3(a)

The tuple below named as tuple_of_words consists of words. Write a function that accepts a word, say word_to_search and the tuple_of_words as arguments, and finds if the word_to_search occurs in the tuple_of_words or not. This is very simple to do with the code word_to_search in tuple_of_words. However, this code is unfortunately very slow.

As the words in the tuple_of_words are already sorted in alphabetical order, we can search using a faster way, called binary search. To implement binary search in a function, start by comparing word_to_search with the middle entry in the tuple_of_words. If they are equal, then you are done and the function should return True. On the other hand, if the word_to_search comes before the middle entry, then search the first half of tuple_of_words. If it comes after the middle entry, then search the second half of tuple_of_words. Then repeat the process on the appropriate half of the tuple_of_words and continue until the word is found or there is nothing left to search, in which case the function short return False. The < and > operators can be used to alphabetically compare two strings.

You may write just one function or multiple functions to solve this problem.

Check your function if the word_to_search is:

  1. 'rocket'

  2. 'rest'

(25 points)